<?php
$categories_html = '';
$categories_1cate_html = '';
$categories_2cate_html = '';
$last_top_cate_id = '';
$categories_output_array = array();
$len = count($link_categories);
$current_num = 0;
foreach ($link_categories as $cate_id => $category) {
    $current_num += 1;
    if ($current_num == $len && $category['c_level'] == 1 && empty($category['has_sub_cate'])) {
        $categories_1cate_html .= '</h2></div>';
        $categories_1cate_html .= '<div class="cate ' .(($current_num == $len)?"last":""). '"><h2 class="item1 ' .($category['sub_count']==0?"emptysub":""). '">';
        $categories_1cate_html .= '<a class="item_title c00" title="' . $category['category_name'] . '" href="' . $category['category_url_link'] . '">' . $category['category_name'] . '</a></h2></div>';
        break;
    }
    $last_one_id = $category['last_one'];
    $next_one_id = $category['next_one'];
    if ($category['c_level'] == 1) {
        $category_banner = $category['category_banner'];
        if (! empty($last_one_id) && isset($link_categories[$last_one_id]) && ! empty($last_top_cate_id)) { //if the last cate exist then close the last one
            $categories_1cate_html .= '</h2></div>';
            $categories_output_array[$last_top_cate_id] = $categories_2cate_html;
            $categories_2cate_html = '';
        }
        $last_top_cate_id = $cate_id;
        $categories_1cate_html .= '<div class="cate"><h2 class="item1 ' .($category['sub_count']==0?"emptysub":""). '">';
        $categories_1cate_html .= '<a class="item_title c00" title="' . $category['category_name'] . '" href="' . $category['category_url_link'] . '">' . $category['category_name'] . '</a>';
        $cate1_sub_count = $category['sub_count'];
        $new_start_column = 0;
    } else {
        if ($new_start_column == 0) {
            $categories_1cate_html .= '<div class="subMenu none" style="display: none;">';
            $categories_1cate_html .= '<div class="subCate">';
        }
        if ($category['c_level'] == 2) {
            $cate2_sub_count = $category['sub_count'];
            $new_start_column2 = 0;
            $new_start_column ++;
            $categories_1cate_html .= '<dl>';
            $categories_1cate_html .= '<dt><a title="' . $category['category_name'] . '" href="' . $category['category_url_link'] . '">' . $category['category_name'] . '</a></dt>';
        } elseif ($category['c_level'] == 3) {
            $new_start_column2 ++;
            if ($new_start_column2 < 6) { // limit lv3 category to 5 column
                $categories_1cate_html .= '<dd><a title="' . $category['category_name'] . '" href="' . $category['category_url_link'] . '">' . $category['category_name'] . '</a></dd>';
            }
        }
        //                print_r($category['c_level'] ==2 && $cate2_sub_count==$new_start_column2);
        if ($cate2_sub_count == $new_start_column2) {
            if((($current_num+1) % 2) == 0) $categories_1cate_html .= '</dl><br class="clear">';
        	else $categories_1cate_html .= '</dl>';
        }
    }
    if ($cate1_sub_count == $new_start_column && $link_categories[$next_one_id]['c_level'] == 1 && ! empty($category['has_sub_cate'])) {
        $categories_1cate_html .= '</div></div>';
    }
    //if(empty($next_one_id) && $last_one_id!=$cate_id)
    if (empty($next_one_id)) {
        $categories_1cate_html .= '</div></div>';
    }
}
//file_put_contents('c:/xx.txt', $categories_1cate_html);
$categories_html = $categories_1cate_html;
$save_c_html = array(
    'top_html' => $categories_1cate_html
);
$categories_output_array = $save_c_html['sub_html'];
$categories_html = $save_c_html['top_html'];
?>